/**
 *
 * Created by Pan,hongxue, 9/14/2011 4:02:21 PM
 *
 */

function validateVideoUrl(ori_url) 
{
    //We use a fake constant duration for yinyuetai.com, because it is MTV, not long duration video
    //TODO: FIXME
    var fcduration = '100';    
	var html = download_page(ori_url);
	var matches = html.match(/var videoId = '(\d+)'/);
	if (matches == null) {
	     // vid not found
	     return 'NotFoundVideo';
	}
	//Dispatcher: http://www.yinyuetai.com/player/get-video-info?videoId=251582&flex=true
	var dst_url = 'http://www.yinyuetai.com/player/get-video-info?flex=true&videoId=' + matches[1];
	var hessian_bin_str = download_page(dst_url);
	var flv_start_pos = hessian_bin_str.lastIndexOf('http://');
	//Note: this is a just hack which could be broken in future version
	var flv_string = hessian_bin_str.substring(flv_start_pos, hessian_bin_str.length - 1);
    var sb = [];
      
    sb.push(flv_string);
    if(sb.length){
       //matches = html.match(/<title>(.*?)<\/title>/);
       // we MUST consider multi-lines case
       matches = html.match(/<title>([\s\S]*?)<\/title>/);
       if(matches){
    	   return 'success_title:' + encodeURIComponent(matches[1])
    	          + '\n video_urls:' + encodeURIComponent(sb)
    	          + '\n seconds:' + Math.round(parseInt(fcduration));
       }
    }
	return 'error:empty_path';
}


/* http://www.yinyuetai.com/video/261605 */
function js_get_media_url(ori_url) 
{
	var html = download_page(ori_url);
	var matches = html.match(/var videoId = '(\d+)'/);
	if (matches == null) {
		// vid not found
		return '';
	}
	//Dispatcher: http://www.yinyuetai.com/player/get-video-info?videoId=251582&flex=true
	var dst_url = 'http://www.yinyuetai.com/player/get-video-info?flex=true&videoId=' + matches[1];
	var hessian_bin_str = download_page(dst_url);
	var flv_start_pos = hessian_bin_str.lastIndexOf('http://');
	//Note: this is a just hack which could be broken in future version
	var flv_string = hessian_bin_str.substring(flv_start_pos, hessian_bin_str.length - 1);

	return flv_string;
}
